<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "tags".
 *
 * @property integer $id
 * @property string $name
 */
class Tags extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'tags';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['name'], 'string', 'max' => 64]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '标签名字',
        ];
    }

    /**
     * 获取与之相关联的QA问答
     * @return type
     */
    public function getQa_tag() {
        return $this->hasMany(Qa_tag::className(), ['tid' => 'id']);
    }

    /**
     * 获取与之相关的QA数量
     */
    public function coount($tid) {
        $num_qa = Qa_tag::find()->where(['tid' => $tid])->count();
        $num_article = Article_tag::find()->where(['tid' => $tid])->count();
        return $num_qa + $num_article;
    }

}
